package com.xinyuan.trade.Adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xinyuan.trade.R;
import com.xinyuan.trade.Activity.BaseActivity;
import com.xinyuan.trade.Activity.NewDetailActivity;
import com.xinyuan.trade.Adaptive.UIAdapter;
import com.xinyuan.trade.Entity.NewEntity;
import com.xinyuan.trade.Utils.Utils;

public class NewAdapter extends BaseAdapter {

	private Context context;
	private BaseActivity mActivity;
	private UIAdapter uiAdapter;
	private List<NewEntity.Data> newlist = new ArrayList<NewEntity.Data>();

	public NewAdapter(BaseActivity activity) {
		this.mActivity = activity;
		this.context = activity;
		this.uiAdapter = UIAdapter.getInstance(activity);
	}

	public void clean() {
		newlist.clear();
	}

	@Override
	public int getCount() {
		return newlist.size();
	}

	@Override
	public Object getItem(int position) {
		return newlist.get(position);
	}

	public void addListEntity(List<NewEntity.Data> list) {
		newlist.addAll(list);
		this.notifyDataSetChanged();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder = null;
		if (view == null) {
			holder = new ViewHolder();
			view = LayoutInflater.from(mActivity).inflate(
					R.layout.layout_newlist_item, null);

			holder.layout_newlist_item = (LinearLayout) view
					.findViewById(R.id.layout_newlist_item);

			holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
			uiAdapter.setTextSize(holder.tv_title, 20);
			uiAdapter.setMargin(holder.tv_title, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 10, 0, 20, 10);

			holder.tv_content = (TextView) view
					.findViewById(R.id.tv_content);
			uiAdapter.setTextSize(holder.tv_content, 20);
			uiAdapter.setMargin(holder.tv_content,
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
					10, 0, 20, 5);

			holder.tv_ptime = (TextView) view
					.findViewById(R.id.tv_ptime);
			uiAdapter.setTextSize(holder.tv_ptime, 18);
			uiAdapter.setMargin(holder.tv_ptime,
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
					10, 0, 20, 0);
		} else
			holder = (ViewHolder) view.getTag();

		final NewEntity.Data data = newlist
				.get(position);

		holder.tv_title.setText(data.getTitle());
		
		holder.tv_content
				.setText(data.getContent());
		holder.tv_ptime.setText("发布时间：" + Utils.formatDataByDay(Integer.parseInt(data.getUpdatetime())));
		
		holder.layout_newlist_item
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startNewDetailActivity(data);
					}
				});

		view.setTag(holder);

		return view;
	}

	class ViewHolder {
		private LinearLayout layout_newlist_item;
		private TextView tv_title;
		private TextView tv_content;
		private TextView tv_ptime;
	}
	
	private void startNewDetailActivity(NewEntity.Data data) {
		Intent intent = new Intent();
		intent.putExtra(NewDetailActivity.EXTRA_NEWID, data);
		intent.setClass(mActivity, NewDetailActivity.class);
		mActivity.startActivity(intent);
		mActivity.overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
	}
}
